$(function(){
    tableUtil.initTable();
});

var tableUtil = {
     initTable:function(){
         $("#table").bootstrapTable({
             url:'/UserFile/selectTable',
             method: "GET",
             toolbar:"#toolbar",
             cache: false,
             striped: true,           //表格显示条纹
             pagination: true,        //在底部显示分页组件
             pageList: [10, 20],       //设置页面可以显示的数据条数
             pageSize: 10,             // 页面数据条数
             pageNumber: 1,            // 首页页码
             sidePagination: 'server', // 设置为服务器端分页
             showColumns:true,        //是否显示内容列下拉框
             showToggle:false,         //切换详细视图和列表视图
             queryParamsType: '',
             queryParams:function (params){   //请求服务器数据时，添加额外参数
                 return {
                     pageSize:params.pageSize, // 每页要显示的数据条数
                     pageNum:params.pageNumber    // 页码
                 }
             },
             idField:'id',             //指定主键列
             //['active', 'success', 'info', 'warning', 'danger'];
             rowStyle: function (row, index) {
                 var style = "";
                 if(row.status == '0'){
                     style='danger';
                 }else{
                     style='active';
                 }
                 return { classes: style }
             },
             columns:[{
                 checkbox: true,   //显示一个勾选框
                 align: 'center',   //居中显示
                 valign: 'middle'
             },{
                 field:'docname',
                 title:'文档名称',
                 align:'center',
                 valign: 'middle',
             },{
                 field:'applyname',
                 title:'申请人',
                 align:'center',
                 valign: 'middle'
             },{
                 field:'applydeptname',
                 title:'所属部门',
                 align:'center',
                 valign: 'middle'
             },{
                 field:'remark',
                 title:'备注',
                 align:'center',
                 valign: 'middle'
             },{
                 field:'submittime',
                 title:'提交时间',
                 align:'center',
                 valign: 'middle',
                 width:100,
                 formatter:function(value,row,index){
                     return value.substring(0,10);
                 }
             }, {
                 field:'authority',
                 title:'已获权限',
                 align:'center',
                 valign: 'middle',
                 formatter:function(value, row, index){
                     if(value== '0'){
                         return "无";
                     }else if(value == "1"){
                         return "浏览";
                     }else if(value == "2"){
                         return "下载";
                     }
                 }
             },{
                 field:'status',
                 title:'审批状态',
                 align:'center',
                 valign: 'middle',
                 formatter:function(value, row, index){
                     if(value == "0"){
                         return "待审批";
                     }else{
                         return "已审批";
                     }
                 }
             },{
                 field:'approverName',
                 title:'审批人',
                 align:'center',
                 valign: 'middle'
             },{
                 field:'time',
                 title:'审批时间',
                 align:'center',
                 valign: 'middle',
                 width:100,
                 formatter:function(value,row,index){
                     return value.substring(0,10);
                 }
             },{
                 field:'id',
                 title:'操作',
                 width:80,
                 align:'center',
                 valign: 'middle',
                 width:100,
                 formatter:function(value, row, index){
                     var id = value;
                     var result = "";
                     result += "<a href='javascript:;' class='btn btn-xs blue' onclick=\"tableUtil.operateById('" + id + "','" + 1 + "' )\" title='浏览权限'><span class='glyphicon glyphicon-expand'></span></a>";
                     result += "<a href='javascript:;' class='btn btn-xs blue' onclick=\"tableUtil.operateById('" + id + "','" + 2 + "' )\" title='下载权限'><span class='glyphicon glyphicon-download'></span></a>";
                     result += "<a href='javascript:;' class='btn btn-xs red'  onclick=\"tableUtil.operateById('" + id + "','" + 0 + "' )\" title='驳回/移除权限'><span class='glyphicon glyphicon-remove'></span></a>";
                     return result;
                 }
             }
             ]
         });
     },
    search:function(){
         var name = $("#name").val();
         $('#table').bootstrapTable('refresh',{ query : { name : name } })
    },
    operateById:function(id,sign){
        $.ajax({
            type:'post',
            url:'/UserFile/operateById',
            data:{ id : id , sign : sign },
            success:function(result){
                if(result.success){
                    $('#table').bootstrapTable('refresh');
                    layer.msg("设置成功");
                }
            },error:function(){
                layer.msg("ajax请求失败！");
            }
        });
    },
}